﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AntikCompta.Data;
using System.Collections;

namespace AntikCompta
{
    public partial class JournalWnd : Form
    {
        private Journal mJournalMng;
        internal JournalWnd(Journal aJournalMng)
        {
            InitializeComponent();

            mJournalMng = aJournalMng;

            ChargerExercices();               
        }

        private void ChargerExercices()
        {
            ICollection annees = mJournalMng.Annees;
            int toSelect = -1;
            if (annees.Count > 0)
            {
                toSelect = 0;
                txt_synthese.Text = "";
            }

            int idx = 0;
            
            foreach (int annee in annees)
            {
                tv_exercices.Nodes.Add(annee + "");
                if (mJournalMng.GetSynthese(annee))
                    txt_synthese.Text += annee + ": OK        ";
                else
                {
                    txt_synthese.Text += annee + ": KO!!!        ";
                    toSelect = idx;
                }
                idx++;
            }

            if (toSelect >= 0)
                tv_exercices.SelectedNode = tv_exercices.Nodes[toSelect];
        }
        

        private void bt_fermer_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tv_exercices_AfterSelect(object sender, TreeViewEventArgs e)
        {
            RefreshView();
        }

        private void RefreshView()
        {
            int annee = Convert.ToInt32(tv_exercices.SelectedNode.Text);

            AfficherResultat(txt_achat, mJournalMng.CheckAchats(annee), bt_achat);
            AfficherResultat(txt_ventes, mJournalMng.CheckVentes(annee), bt_ventes);
            AfficherResultat(txt_frais, mJournalMng.CheckFrais(annee), bt_frais);
            AfficherResultat(txt_caisse, mJournalMng.CheckCaisse(annee), bt_caisse);
        }

        private void AfficherResultat(Label aLabel, bool aRes, Button aButton)
        {
            aLabel.ForeColor = (aRes ? Color.Green : Color.Red);
            aLabel.Text = (aRes ? "OK" : "KO");
            aButton.Enabled = !aRes;
        }

        private void bt_achat_Click(object sender, EventArgs e)
        {
            int annee = Convert.ToInt32(tv_exercices.SelectedNode.Text);
            List<String> erreurs = mJournalMng.ErreursAchats(annee);
            ErreursWnd wnd = new ErreursWnd("achats", annee, erreurs);
            wnd.ShowDialog(this);
        }

        private void bt_ventes_Click(object sender, EventArgs e)
        {
            int annee = Convert.ToInt32(tv_exercices.SelectedNode.Text);
            List<String> erreurs = mJournalMng.ErreursVentes(annee);
            ErreursWnd wnd = new ErreursWnd("ventes", annee, erreurs);
            wnd.ShowDialog(this);
        }

        private void bt_caisse_Click(object sender, EventArgs e)
        {
            int annee = Convert.ToInt32(tv_exercices.SelectedNode.Text);
            List<String> erreurs = mJournalMng.ErreursCaisses(annee);
            ErreursWnd wnd = new ErreursWnd("caisse", annee, erreurs);
            wnd.ShowDialog(this);
        }

        private void bt_frais_Click(object sender, EventArgs e)
        {
            int annee = Convert.ToInt32(tv_exercices.SelectedNode.Text);
            List<String> erreurs = mJournalMng.ErreursFrais(annee);
            ErreursWnd wnd = new ErreursWnd("frais", annee, erreurs);
            wnd.ShowDialog(this);
        }
    }
}
